package com.febers.ihealth

import android.widget.RadioButton
import androidx.appcompat.widget.Toolbar
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import com.febers.ihealth.base.BaseActivity
import com.febers.ihealth.utils.*
import kotlinx.android.synthetic.main.activity_base_info.*
import kotlinx.android.synthetic.main.toolbar_common.*

class BaseInfoActivity: BaseActivity() {

    private var chooseGender = false

    override fun setView(): Int = R.layout.activity_base_info

    override fun setToolbar(): Toolbar? = toolbar_common

    override fun setToolbarTitle(): String? = "基本信息"

    override fun afterCreate() {
        val radioButtonFemale: RadioButton = findViewById(R.id.radio_button_female)
        if (user.baseInfoFinish()) {
            edit_text_age.setText(user.age)
            edit_text_height.setText(user.height)
            edit_text_weight.setText(user.weight)
            edit_text_hot.setText(user.temp)
            edit_text_heart.setText(user.heart)
            edit_text_pressure.setText(user.pressure)
            if (user.male) {
                radio_group_base_survey.check(R.id.radio_button_male)
            } else {
                radio_group_base_survey.check(R.id.radio_button_female)
                if (user.preg) {
                    radioButtonFemale.text = "女性（孕妇）"
                } else {
                    radioButtonFemale.text = "女性"
                }
            }
            chooseGender = true
        }
        radio_group_base_survey.setOnCheckedChangeListener { radioGroup, id ->
            chooseGender = true
            when(id) {
                R.id.radio_button_male -> {
                    log("is male")
                    user.male = true
                }
                R.id.radio_button_female -> {
                    log("not male")
                    user.male = false
                    MaterialDialog(this)
                        .title(text = "请问您是否为孕妇？")
                        .show {
                            listItemsSingleChoice(items = arrayListOf("是", "否")) { dialog, index, text ->
                                when(index) {
                                    0 -> {
                                        user.preg = true
                                        radioButtonFemale.text = "女性（孕妇）"
                                    }
                                    1 -> {
                                        user.preg = false
                                        radioButtonFemale.text = "女性"
                                    }
                                }
                            }
                        }
                    }
            }
        }
        fab_check_base_info.setOnClickListener {
            user.age = edit_text_age.text.toString()
            user.height = edit_text_height.text.toString()
            user.weight = edit_text_weight.text.toString()
            user.temp = edit_text_hot.text.toString()
            user.heart = edit_text_heart.text.toString()
            user.pressure = edit_text_pressure.text.toString()
            user.fluTimes = edit_text_flu_times.text.toString()

            if (user.baseInfoFinish() && chooseGender) {
                IOUtil.saveUser(user)
                nav(BeforeSurveyAct::class.java)
                finish()
            } else {
                toast("您还没有完成所有内容的填写")
            }
        }
    }

}